<template>
    <view>
        <uni-segmented-control :current="menuIndex" :values="menuItems" style-type="text" active-color="#007aff" @clickItem="onClickItem" />

        <uni-list v-if="menuIndex === 0">
            <uni-list-item v-for="(item, index) in orderQuantity" :key="index">
                <template v-slot:header>
                    <uni-tag :text="index + 1" :type="getNo(index)" size="normal" />
                </template>
                <template v-slot:body>
                    <view style="flex: 1; margin-left: 10px">{{ item.orName + "(" + (item.store?.osName || " ") + ")" }}</view>
                </template>
                <template v-slot:footer>
                    <view>{{ numberFormat(item.sfShao, 0) }}</view>
                </template>
            </uni-list-item>
            <u-empty v-if="orderQuantity.length === 0" text="暂无数据"> </u-empty>
        </uni-list>

        <uni-list v-if="menuIndex === 1">
            <uni-list-item v-for="(item, index) in orderAmount" :key="index">
                <template v-slot:header>
                    <uni-tag :text="index + 1" :type="getNo(index)" size="normal" />
                </template>
                <template v-slot:body>
                    <view style="flex: 1; margin-left: 10px">{{ item.orName + "(" + (item.store?.osName || " ") + ")" }}</view>
                </template>
                <template v-slot:footer>
                    <view>¥{{ numberFormat(item.sfSham, 2) }}</view>
                </template>
            </uni-list-item>
            <u-empty v-if="orderAmount.length === 0" text="暂无数据"> </u-empty>
        </uni-list>

        <uni-list v-if="menuIndex === 2">
            <uni-list-item v-for="(item, index) in shareQuantity" :key="index">
                <template v-slot:header>
                    <uni-tag :text="index + 1" :type="getNo(index)" size="normal" />
                </template>
                <template v-slot:body>
                    <view style="flex: 1; margin-left: 10px">{{ item.orName + "(" + (item.store?.osName || " ") + ")" }}</view>
                </template>
                <template v-slot:footer>
                    <view>{{ item.sfShat }}</view>
                </template>
            </uni-list-item>
            <u-empty v-if="shareQuantity.length === 0" text="暂无数据"> </u-empty>
        </uni-list>
    </view>
</template>

<script>
import { getStaffRanks } from "../../utils/api";
import { mapGetters } from "vuex";
import {getNo} from "../../utils/helper.js"

export default {
    data() {
        return {
            menuItems: ["订单数", "订单总额", "分享次数"],
            menuIndex: 0,
            staffRank: [],
            orderQuantity: [],
            orderAmount: [],
            shareQuantity: [],
        };
    },

    onLoad() {
        this.loadStaffRank({
            gpUuid: this.activity?.gpUuid,
            orderBy: "SF_SHAO_DESC",
            first: 20,
            offset: 0,
        });
    },

    computed: {
        ...mapGetters({
            activity: "activityInfo/activity",
        }),
    },

    methods: {
        onClickItem(e) {
            if (this.menuIndex !== e.currentIndex) {
                this.menuIndex = e.currentIndex;
                if (e.currentIndex == 0) {
                    this.loadStaffRank({
                        gpUuid: this.activity?.gpUuid,
                        orderBy: "SF_SHAO_DESC",
                        first: 20,
                        offset: 0,
                    });
                }
                if (e.currentIndex == 1) {
                    this.loadStaffRank({
                        gpUuid: this.activity?.gpUuid,
                        orderBy: "SF_SHAM_DESC",
                        first: 20,
                        offset: 0,
                    });
                }
                if (e.currentIndex == 2) {
                    this.loadStaffRank({
                        gpUuid: this.activity?.gpUuid,
                        orderBy: "SF_SHAT_DESC",
                        first: 20,
                        offset: 0,
                    });
                }
            }
        },

        async loadStaffRank({ gpUuid, orderBy, first = 20, offset = 0 }) {
            let res = await getStaffRanks({
                gpUuid,
                orderBy,
                first,
                offset,
            });
            if (orderBy === "SF_SHAO_DESC") this.orderQuantity = res?.ranks?.filter((item) => item.sfShao > 0) || [];
            if (orderBy === "SF_SHAM_DESC") this.orderAmount = res?.ranks?.filter((item) => item.sfSham > 0) || [];
            if (orderBy === "SF_SHAT_DESC") this.shareQuantity = res?.ranks?.filter((item) => item.sfShat > 0) || [];
        },
    },
};
</script>

<style lang="scss" scoped></style>
